<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/17 11:00
 * Desc:
 */
class Service_Api_Member_Guide extends Contract_Api_Member_Guide
{

    public function is_guide($mid): array
    {
        $guide = Model_Api_V2_Guide::get_guide_by_mid($mid);
        if (empty($guide) || $guide['status'] != 1) {
            return array(
                'is_guide' => 0,
                'msg' => '未开通导游/认证未通过'
            );
        }

        return array(
            'is_guide' => 1,
            'litpic' => $this->img_url($guide['litpic']),
            'title' => $guide['title'],
        );
    }

    public function certify_status($mid): array
    {
        $guide = Model_Api_V2_Guide::get_guide_by_mid($mid);
        if (empty($guide)) {
            return array(
                'status' => -1
            );
        }

        return array(
            'status' => $guide['status']
        );
    }

    public function certify($mid, $params): array
    {

        /**
         * @var $guide_code
         * @var $guide_pic
         */
        extract($params);

        $member = DB::select()->from('member')->where('mid', '=', $mid)->execute()->current();
        if ($member['verifystatus'] != 2) {
            throw new Exception('请先实名认证');
        }

        // 检查导游编号是否存在
        $check_guide = DB::select()
            ->from('guide')
            ->where('guide_card_id', '=', $guide_code)
            ->where('mid', '!=', $mid)
            ->execute()
            ->valid();
        if ($check_guide) {
            throw new Exception('导游编号已存在');
        }

        $guide = Model_Api_V2_Guide::get_guide_by_mid($mid);
        if ($guide['status'] == 1) {
            throw new Exception('已通过审核');
        }

        $update_data = array(
            'title' => $member['truename'],
            'mobile' => $member['mobile'],
            'litpic' => $member['litpic'],
            'guide_card_id' => $guide_code,
            'guide_card_pic' => substr($guide_pic, strpos($guide_pic, "/upload")),
            'mid' => $mid,
            'status' => 3
        );
        if ($guide) {
            $update_data['modtime'] = time();
            DB::update('guide')->set($update_data)->where('id', '=', $guide['id'])->execute();
        } else {
            $update_data['addtime'] = time();
            DB::insert('guide', array_keys($update_data))->values(array_values($update_data))->execute();
        }

        return array(
            'status' => true,
        );
    }

    public function get_qrcode($mid): array
    {
        $guide = Model_Api_V2_Guide::check_guide_is_available($mid);

        if (!$guide['guide_qrcode']) {
            throw new Exception('未生成导游码');
        }

        return array(
            'guide_qrcode' => $this->img_url($guide['guide_qrcode'])
        );
    }

    public function record($mid, $params): array
    {
        /**
         * @var $keyword
         * @var $pagesize
         * @var $page
         */
        extract($params);

        $guide = Model_Api_V2_Guide::check_guide_is_available($mid);

        $offset  = St_Page::offset($page, $pagesize);
        $builder = DB::select('a.ordersn', 'a.productname', 'a.linkman', 'a.litpic', 'a.status', 'a.productautoid',
            'a.typeid', 'a.addtime', 'a.usedate', 'b.people_num', 'b.start_time', 'b.end_time', 'b.award')
            ->from(array('member_order', 'a'))
            ->join(array('guide_order', 'b'), 'inner')
            ->on('a.ordersn', '=', DB::expr('sline_b.ordersn'))
            ->where('b.guide_id', '=', $guide['id']);

        if (!empty($keyword)) {
            $builder->where('a.ordersn', '=', $keyword);
        }

        $total = $builder->execute()->count();
        $list  = $builder->order_by('a.addtime', 'DESC')->offset($offset)
            ->limit($pagesize)
            ->execute()
            ->as_array();

        foreach ($list as &$v) {
            $v['litpic']      = $this->img_url($v['litpic']);
            $v['addtime']     = date('Y-m-d H:i:s', $v['addtime']);
            $v['status_name'] = Model_Guide::get_order_status_name($v['status']);
            $v['guide']       = $guide['title'];
            $v['start_time']  = $v['start_time'] ? date('Y-m-d H:i:s', $v['start_time']) : '';
            $v['end_time']    = $v['end_time'] ? date('Y-m-d H:i:s', $v['end_time']) : '';
        }

        return array('total' => $total, 'list' => $list);
    }

    public function scan_code($mid, $params): array
    {
        $guide = Model_Api_V2_Guide::check_guide_is_available($mid);

        $code  = $params['code'];
        $order = DB::select()
            ->from('member_order')
            ->where('eticketno', '=', $code)
            ->and_where('typeid', '=', Model_Api_V2_Guide::TYPE_ID)
            ->where_open()
            ->where('status', '=', 2)
            ->or_where('status', '=', 9)
            ->where_close()
            ->execute()
            ->current();

        if (!isset($order['id'])) {
            throw new Exception('未找到订单');
        }

        $guide_order = DB::select()
            ->from('guide_order')
            ->where('ordersn', '=', $order['ordersn'])
            ->and_where('guide_id', '=', $guide['id'])
            ->execute()
            ->current();

        if (!isset($guide_order['id'])) {
            throw new Exception('此订单你不能接单');
        }

        $data = array(
            'ordersn' => $order['ordersn'],
            'title' => $order['productname'],
            'usedate' => $order['usedate'],
            'guide' => $guide['title'],
            'code' => $code,
            'tourer' => $order['linkman'],
            'status' => $order['status']
        );
        if ($guide_order['start_time']) {
            $data['start_time'] = date('Y-m-d H:i:s', $guide_order['start_time']);
        }
        if ($guide_order['end_time']) {
            $data['end_time'] = date('Y-m-d H:i:s', $guide_order['end_time']);
        }

        return $data;
    }

    public function start_service($mid, $params): array
    {
        $guide = Model_Api_V2_Guide::check_guide_is_available($mid);

        $ordersn = $params['ordersn'];

        if (!$ordersn) {
            throw new Exception('订单号不能为空');
        }

        $guide_order = DB::select()
            ->from('guide_order')
            ->where('ordersn', '=', $ordersn)
            ->and_where('guide_id', '=', $guide['id'])
            ->execute()
            ->current();

        if (!isset($guide_order['id'])) {
            throw new Exception('未找到订单');
        }

        $order = Model_Member_Order::get_order_by_ordersn($ordersn);
        if ($order['status'] != 2) {
            throw new Exception('接单失败,此订单为非待接单状态');
        }

        //时间判断
        $use_date = strtotime($order['usedate']);
        if ($use_date != strtotime(date('Y-m-d'))) {
            throw new Exception('接单失败,服务日期有误');
        }

        //更新订单状态
        DB::update('member_order')->set(array('status' => 9))->where('ordersn', '=', $ordersn)->execute();

        //更新服务开始时间
        DB::update('guide_order')->set(array('start_time' => time()))->where('ordersn', '=', $ordersn)->execute();

        return array('status' => true);
    }

    public function end_service($mid, $params): array
    {
        $guide = Model_Api_V2_Guide::check_guide_is_available($mid);

        $ordersn = $params['ordersn'];
        if (!$ordersn) {
            throw new Exception('订单号不能为空');
        }

        $guide_order = DB::select()
            ->from('guide_order')
            ->where('ordersn', '=', $ordersn)
            ->and_where('guide_id', '=', $guide['id'])
            ->execute()
            ->current();

        if (!isset($guide_order['id'])) {
            throw new Exception('未找到订单');
        }

        $order = Model_Member_Order::get_order_by_ordersn($ordersn);
        if ($order['status'] != 9) {
            throw new Exception('接单失败,此订单为非服务中状态');
        }

        Model_Guide::end_service($ordersn);

        return array('status' => true,);
    }
}